package org.fizz.timertask;

import org.fizz.handlerequest.LinuxLocalConfigIml;
import org.fizz.network.CumtNetWorkClientImp;
import org.fizz.network.NoSuchOperatorName;
import org.fizz.network.OperatorName;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import static org.fizz.handlerequest.ConfigKeyConstant.ipV4Name;
import static org.fizz.handlerequest.ConfigKeyConstant.macName;

/**
 * @Author Fizz Pu
 * @Date 2021/6/10 下午5:44
 * @Version 1.0
 * 失之毫厘，缪之千里！
 */

@Component
public class Task {

    String userName = "16184876";
    String passWord = "147934";
    OperatorName operatorName = OperatorName.Unicom;
    CumtNetWorkClientImp cumtNetWorkClientImp = new CumtNetWorkClientImp(userName, passWord, operatorName);
    LinuxLocalConfigIml linuxLocalConfigIml = new LinuxLocalConfigIml();

    public Task() throws SocketException {

    }

    // 每天早上8点触发一次执行一次任务, 每隔30分钟执行一次
    @Scheduled(cron="0 0,30,59 8-23 * * ?")
    public void execute() throws NoSuchOperatorName {
        for (Map<String, String> map : (Iterable<Map<String, String>>) linuxLocalConfigIml) {
            String ipV4 = map.get(ipV4Name);
            String mac = map.get(macName);
            cumtNetWorkClientImp.setIpV4(ipV4);
            cumtNetWorkClientImp.setMacAddress(mac);
            if (cumtNetWorkClientImp.login()) {
                System.out.println("登录成功");
                return;
            }
        }
        System.out.println("登录失败");
    }
}
